package com.situ.dao;

import java.util.List;

import com.situ.entily.Commodity;
import com.situ.util.DBUtil;

public class CommodityDao {

	

	public List<Commodity> all(int index, int limit, String keyword) {
		String sql;
		if (keyword == null || "".equals(keyword)) {
			sql = "select * from commodity where comm_del=0 limit ?,?";
		} else {
			sql = "select * from commodity where instr(comm_name,'" + keyword + "')>0 and comm_del=0 limit ?,?";
		}
		return DBUtil.executeDQL(sql, Commodity.class, index, limit);
	}

	public Long count(String keyword) {
		String sql = "select count(1) from commodity where comm_del=0";
		if (keyword != null && !"".equals(keyword)) {
			sql += " and instr(comm_name,'" + keyword + "')>0";
		}
		return DBUtil.executeCount(sql);
	}

	public Commodity selectById(Integer id) {
		String sql = "select * from commodity where comm_id=?";

		List<Commodity> list = DBUtil.executeDQL(sql, Commodity.class, id);
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	public void insert(Commodity c) {
		String sql = "insert into commodity(comm_name,comm_manufacturer,comm_marking,comm_specifications,comm_quantity) value(?,?,?,?,?)";
		DBUtil.executeDML(sql, c.getCommName(),c.getCommManufacturer(),c.getCommMarking(),c.getCommSpecifications(),c.getCommQuantity());
		
	}

	public void update(Commodity c) {
		String sql = "update commodity set comm_name=?,comm_manufacturer=?,comm_marking=?,comm_specifications=?,comm_quantity=? where comm_id=?";
		DBUtil.executeDML(sql, c.getCommName(),c.getCommManufacturer(),c.getCommMarking(),c.getCommSpecifications(),c.getCommQuantity(),c.getCommId());
		
	}

	public void delete(Integer id) {
		//String sql = "delete from commodity where comm_id=?";
		String sql = "update commodity set comm_del=1 where comm_id=?";
		DBUtil.executeDML(sql, id);		
	}

	public List<Commodity> selall() {
		String sql = "select * from commodity where comm_del=0";
		return DBUtil.executeDQL(sql,Commodity.class);
	}
	
}
